package link.hwx.controller.u9;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import link.hwx.entity.oa.OaBillInfoVo;
import link.hwx.service.oa.OaBillingService;
import link.hwx.service.u9.BillingService;
import link.hwx.util.AjaxResult;
import link.hwx.util.CommonSetData;
import link.hwx.util.HttpManager;
import link.hwx.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * srm支票单接口
 */
@RestController
@RequestMapping("/cx/srm")
public class SrmBillingOpenApi {
    @Autowired
    private BillingService billingService;
    @Autowired
    private OaBillingService oaBillingService;
    //审核通过
    @PostMapping("/sApprovedBilling")
    public AjaxResult uApprovedpoa(@RequestBody Map<String, Object> jsonStr) {
        if (jsonStr == null) {
            Map<String, Object> result = new HashMap<String, Object>();
            return AjaxResult.error("数据不能为空!");
        }

        //通过requestid获取oa单据
        String requestId = (String) jsonStr.get("requestId");
        OaBillInfoVo oaPrInfoVo = oaBillingService.findBillingByRequestId(CommonSetData.oaBillingTableMainDbNameD, requestId);
        //有srm单据号直接审核，无则创建并审核
        if (oaPrInfoVo != null) {
            jsonStr.put("shyj", "1");
            String result = approvedPr(jsonStr);
            if (StringUtils.isNotEmpty(result)) {
                JSONObject resultmap = JSON.parseObject(result);
                if (resultmap.getBoolean("Success")) {
                    return AjaxResult.success();

                }else{
                    return AjaxResult.error(resultmap.getString("Message"));
                }
            }
        }
        return AjaxResult.error();
    }
    //退回
    @PostMapping("/sRejBilling")
    public AjaxResult oaRejBilling(@RequestBody Map<String, Object> jsonStr) {
        if (jsonStr == null) {
            Map<String, Object> result = new HashMap<String, Object>();
            return AjaxResult.error("数据不能为空!");
        }

        //通过requestid获取oa单据
        String requestId = (String) jsonStr.get("requestId");
        OaBillInfoVo oaPrInfoVo = oaBillingService.findBillingByRequestId(CommonSetData.oaBillingTableMainDbNameD, requestId);
        //有srm单据号直接审核
        if (oaPrInfoVo != null) {
            jsonStr.put("shyj", "2");
            String result = approvedPr(jsonStr);
            if (StringUtils.isNotEmpty(result)) {
                JSONObject resultmap = JSON.parseObject(result);
                if (resultmap.getBoolean("Success")) {


                    return AjaxResult.success();

                }else{
                    return AjaxResult.error(resultmap.getString("Message"));
                }
            }
        }
        return AjaxResult.error();
    }

     //审核
    public String approvedPr(Map<String, Object> jsonStr) {
        String srmUrl = CommonSetData.serveSrmIp + "/sysplat/dataget/data.ashx?type=webapi&optype=setflowstate";
        String userpwd = StringUtil.md5(CommonSetData.srmUserPwd);
        String shyj = String.valueOf(jsonStr.get("shyj"));//审核意见
        JSONObject user = new JSONObject();
        user.put("usercode", CommonSetData.srmUserCode);
        user.put("userpwd", userpwd);
        JSONObject flowstate = new JSONObject();
        flowstate.put("voucherno", "focus_srm_billing");
        flowstate.put("VoucherID", jsonStr.get("srmid"));
        flowstate.put("ApprovalStatus", shyj);
        Map<String, Object> currentUser = (Map<String, Object>) jsonStr.get("currentUser");
        String apprName = (String) currentUser.get("lastname");//当前操作人名 用作审核人
        flowstate.put("Approver", apprName);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        flowstate.put("ApproverDate", sdf.format(new Date()));
        flowstate.put("ApprovalOpinion","");
       // JSONObject flowLins = new JSONObject();

        JSONObject datatable = new JSONObject();
        datatable.put("user", user);
        datatable.put("flowstate", flowstate);
        //datatable.put("FlowLins", flowLins);
        String data = JSON.toJSONString(datatable);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create() ;
        ContentType contentType = ContentType.create("text/plain", Charset.forName("UTF-8"));
        builder.addTextBody("data", data, contentType);
        String s1 = HttpManager.sendPostFormData(srmUrl, builder,data);

        return s1;
    }


}